using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Home_Courses : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        this.BuildUnitsForCourseLabel();
    }

    protected void btnInsertCourse_Click(object sender, EventArgs e)
    {
        PlacementManager.CourseComponent oCourseComponent = new PlacementManager.CourseComponent();
        PlacementManager.Course oNewCourse = new PlacementManager.Course();

        oNewCourse.CourseCode = this.txtCourseCode.Text.Trim();
        oNewCourse.CourseName = this.txtCourseName.Text.Trim();
        oNewCourse.CreatedBy = User.Identity.Name;
        oNewCourse.DateCreated = DateTime.Now;
        oNewCourse.DateUpdated = DateTime.Now;
        oNewCourse.DivisionId = Int32.Parse(Session["DivisionId"].ToString());
        oNewCourse.IsActive = true;
        oNewCourse.UpdatedBy = User.Identity.Name;

        oCourseComponent.Insert(oNewCourse);

        this.gvCourses.DataBind();
        this.txtCourseCode.Text = string.Empty;
        this.txtCourseName.Text = string.Empty;
    }

    protected void gvCourses_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PlacementManager.CourseComponent oCourseComponent = new PlacementManager.CourseComponent();
        PlacementManager.Course oCourse = new PlacementManager.Course();
        oCourse = oCourseComponent.SelectOne((int)this.gvCourses.SelectedDataKey.Value);

        e.NewValues["DateCreated"] = oCourse.DateCreated;
        e.NewValues["DivisionId"] = oCourse.DivisionId;
        e.NewValues["DateUpdated"] = DateTime.Now;
        e.NewValues["UpdatedBy"] = User.Identity.Name;
    }

    protected void btnDeAllocate_Click(object sender, EventArgs e)
    {

    }

    protected void btnAllocate_Click(object sender, EventArgs e)
    {

    }



    protected void btnAddCourse_Click(object sender, EventArgs e)
    {
       
    }

    



    protected void btnShowDetails_Click(object sender, EventArgs e)
    {
        
    }

    protected void btnAddUnitsToCourse_Click(object sender, EventArgs e)
    {
        PlacementManager.LinkCourseUnitComponent oLinkCourseUnitC = new PlacementManager.LinkCourseUnitComponent();
        int courseId = Int32.Parse(this.gvCourses.SelectedDataKey.Value.ToString());

        foreach (ListItem li in this.lbAvailableUnitsForCourse.Items)
        {
            if (li.Selected)
            {
                int unitId = Int32.Parse(li.Value);
                PlacementManager.LinkCourseUnit oNewCourseUnitLink = new PlacementManager.LinkCourseUnit();
                oNewCourseUnitLink.CourseId = courseId;
                oNewCourseUnitLink.CreatedBy = User.Identity.Name;
                oNewCourseUnitLink.DateCreated = DateTime.Now;
                oNewCourseUnitLink.DateUpdated = DateTime.Now;
                oNewCourseUnitLink.UnitId = unitId;
                oNewCourseUnitLink.UpdatedBy = User.Identity.Name;

                oLinkCourseUnitC.Insert(oNewCourseUnitLink);

            }
        }

        this.gvUnits.DataBind();
        this.lbAvailableUnitsForCourse.DataBind();
    }

    protected void gvUnits_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteUnitLink")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int unitId = Int32.Parse(this.gvUnits.DataKeys[index].Value.ToString());
            int courseId = Int32.Parse(this.gvCourses.SelectedDataKey.Value.ToString());

            PlacementManager.LinkCourseUnitComponent oLinkUnitCourseC = new PlacementManager.LinkCourseUnitComponent();
            oLinkUnitCourseC.DeleteAllWUnitIdAndCourseId(courseId, unitId);

            this.gvUnits.DataBind();
            this.lbAvailableUnitsForCourse.DataBind();
        }
    }

    protected void gvCourses_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BuildUnitsForCourseLabel();
    }

    protected void BuildUnitsForCourseLabel()
    {
        if (this.gvCourses.Rows.Count > 0)
        {
            GridViewRow selectedRow = this.gvCourses.SelectedRow;
            this.lblUnitsForCourse.Text = "Units for " + selectedRow.Cells[1].Text + ":";
        }
    }
    protected void gvCourses_DataBound(object sender, EventArgs e)
    {
        if (this.gvCourses.Rows.Count > 0)
        {
            this.fvCourse.Visible = true;
            this.lblNoCourseSelected.Visible = false;
            
            this.lblNoCourseForUnits.Visible = false;
        }
        else
        {
            this.fvCourse.Visible = false;
            this.lblNoCourseSelected.Visible = true;
            
            this.lblNoCourseForUnits.Visible = true;
        }
    }
    protected void lbAvailableUnitsForCourse_DataBound(object sender, EventArgs e)
    {
        if (this.lbAvailableUnitsForCourse.Items.Count > 0)
        {
            this.lbAvailableUnitsForCourse.Visible = true;
            this.btnAddUnitsToCourse.Visible = true;
            this.lblNoUnitsAvailable.Visible = false;
        }
        else
        {
            this.lbAvailableUnitsForCourse.Visible = false;
            this.btnAddUnitsToCourse.Visible = false;
            this.lblNoUnitsAvailable.Visible = true;
        }
    }
    protected void fvCourse_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        PlacementManager.CourseComponent oCourseC = new PlacementManager.CourseComponent();
        PlacementManager.Course oCourse = oCourseC.SelectOne(Int32.Parse(this.gvCourses.SelectedDataKey.Value.ToString()));

        e.NewValues.Add("CreatedBy", oCourse.CreatedBy);
        e.NewValues.Add("DateCreated", oCourse.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("DivisionId", oCourse.DivisionId);
        e.NewValues.Add("CourseId", oCourse.CourseId);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }
    protected void fvCourse_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        this.gvCourses.DataBind();
        this.BuildUnitsForCourseLabel();
    }
    protected void gvCourses_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string venueName = DataBinder.Eval(e.Row.DataItem, "CourseName").ToString();

            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + venueName + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
        }
    }
    protected void gvUnits_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string venueName = DataBinder.Eval(e.Row.DataItem, "UnitName").ToString();

            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + venueName + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
        }
    }
}
